﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using OnlineBookReader;
using HtmlExtractor;
using Reader.Domain;
using SLHtmlExtractor.ObjectOutput;
using System.Text;
using System.Windows.Browser;

namespace Reader.View
{
    [Sample("搜索", "SearchBook")]
    public partial class SearchBook : UserControl
    {
        public SearchBook()
        {
            InitializeComponent();
        }
        public SearchBook(string uri)
            : this()
        {
            //Uri = uri;
        }
        private string searchKey = "CurrentSearch";
        private string searchBook = "CurrentSearchBook";
        private string searchSite = "CurrentSearchSite";
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string bookName= HttpUtility.UrlEncode(textBox1.Text.Trim());
            if (!string.IsNullOrEmpty(bookName))
            {
                string searchUri = "http://www.baidu.com/s?q1={0}&q6={1}&ie=utf-8";
                var o = comboBox1.SelectedItem as ComboBoxItem;
                Cache.Current.Add(searchBook, textBox1.Text.Trim());
                Cache.Current.Add(searchSite, o);
                searchUri = string.Format(searchUri, bookName, o.Tag.ToString());
                try
                {
                    WebClient wc = new WebClient();
                    wc.Encoding = new Gb2312Encoding();
                    wc.DownloadStringAsync(new Uri(searchUri));
                    wc.DownloadStringCompleted += (s, args) =>
                    {
                        if (args.Error == null)
                        {
                            ParserContext context = new ParserContext(args.Result, searchUri);
                            string extractorFile = @"Resource/ObjectExtractor.config";
                            Extractor extr = new Extractor(extractorFile);
                            PageResult result;
                            extr.TryGetResult(context, out result);
                            object b = new SearchResults();
                            ConvertToObject ct = new ConvertToObject();
                            ct.TryGetOutPutResult(result, ref b);
                            SearchResults bp = (SearchResults)b;
                            listBox1.DataContext = bp.AllResult;
                            Cache.Current.Add(searchKey, bp);
                            SampleBrowser.Current.PopulateSamplesTab("SearchBook", searchUri);
                        }
                    };
                }
                catch
                { }
            }
        }
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hb = e.OriginalSource as HyperlinkButton;
            if (hb.Tag.ToString()!= "none")
            {
                SampleBrowser.Current.PopulateSamplesTab(hb.Tag.ToString(), hb.CommandParameter.ToString());
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            SearchResults result = null;
           if(Cache.Current.TryGet(searchKey,out result))
           {   
               listBox1.DataContext = result.AllResult;
               textBox1.Text = Cache.Current.Get<string>(searchBook);
               comboBox1.SelectedItem = Cache.Current.Get(searchBook);
           }
        }
    }

}
